import React, { Component } from 'react';
import { Button, message } from 'antd';
import { messages } from 'share/common';
import AdvancedTable from 'components/advanced-table';
import invoiceManagementService from '../invoice-management.service';
import { tableDecorator } from 'containers/supplier-management/invoice-management/decorator';
import SlideFrame from 'share/openSlideFrame';
import NewExpirationReminder from 'containers/supplier-management/invoice-management/invoice-check-detail/new-expiration'

const { SimpleTable } = AdvancedTable;

@tableDecorator
class OverdueTable extends Component {
  state = {
    columns: ['description', 'receiptTypeList', 'taxCodeList', 'forceEnabled', 'enabled', 'operation'],
    searchColumns: []
  };

  overdueRef = null;

  getOverdueService = (page, pageSize, searchParams) => {
    const { id } = this.props;
    return invoiceManagementService.getOverdueList( id,{
      page,
      pageSize,
      ...searchParams
    });
  };

  handleDelete = async (record) => {
    this.setState({ loading: true });
    try {
      await invoiceManagementService.deleteExpirationReminder(record.id);
      message.success(messages('common.delete.successful')/*删除成功*/);
      this.overdueRef.getList();
    }
    catch (e) {

    }
    this.setState({ loading: true });
  };

  handleNew = () => {
    let recordExpiration = {
      expiryRemindDay: 10,
      i18n: {},
      staticCalibrationFlag: true,
      expiryDay: 120,
      dynamicRuleFailureTime: '12-31',
      enabled: false,
      forceEnabled: false,
      effectiveTime: new Date().format('MM-dd')
    };
    this.handleEdit(recordExpiration);
  };

  handleEdit = (recordExpiration) => {
    const { id } = this.props;
    SlideFrame.openSlideFrame({
      title: !recordExpiration.id ? messages('expense-3.key172')/*新增发票逾期校验规则*/ : messages('expense-3.key173')/*编辑发票逾期校验规则*/,
      content: NewExpirationReminder,
      params: { recordExpiration ,id},
      afterClose: refresh => typeof refresh === 'boolean' && refresh && this.overdueRef.getList()
    }, this);
  };



  render() {
    const { columns, searchColumns } = this.state;
    return (
      <SimpleTable
        onRow={record => ({
          onClick: () => this.handleEdit(record)
        })}
        onRef={ref => this.overdueRef = ref}
        rowKey='id'
        columns={this.handleColumns(columns)}
        service={this.getOverdueService}
        searchForm={this.handleSearchForm(searchColumns)}
      >
        <Button type="primary" onClick={this.handleNew}>
          {messages('common.create')/*新建*/}
        </Button>
      </SimpleTable>
    );
  }
}

OverdueTable.propTypes = {};
export default OverdueTable;
